package com.yty.myadmin.controler;

import com.yty.myadmin.dao.SysUserDAO;
import com.yty.myadmin.dao.TESTDAO;
import com.yty.myadmin.domain.SysUser;
import com.yty.myadmin.domain.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextImpl;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/test")
public class TEST {

    @Autowired
    private SysUserDAO sysUserDAO;
    @Autowired
    private TESTDAO testdao;
    @RequestMapping("t1")
    SysUser getRole(
            @RequestParam(name = "t") String name
    ){
        return  sysUserDAO.findByUserName(name);
    }

    @RequestMapping("t2")
    Map<String, Object> getTest(HttpServletRequest request){
//        SecurityContextImpl securityContext = (SecurityContextImpl)
//                request.getSession().getAttribute("SPRING_SECURITY_CONTEXT");
//        String name = securityContext.getAuthentication().getName();
        List<Test> tests = testdao.findByName("12");
        Map<String,Object> map = new HashMap<>();
        if(tests == null){
            map.put("error",null);
            return map;
        }
        int i = 0;
        for(Test test: tests){
            map.put("t"+i,test);
            i+=1;
        }
        return map;
    }
}
